/****************************************************************************
*  Arthea Server by R. Jennings (2007-2008)   http://arthea.googlecode.com/ *
*  By using this code you comply with the Artistic and GPLv2 Licenses.      *
****************************************************************************/


using System;
using System.Reflection;

namespace Arthea.Updates
{
    /// <summary>
    /// Handles the threading of the update methods
    /// </summary>
    public struct UpdateManager
    {
        /// <summary>
        /// How often areas update.
        /// </summary>
        public const int PulseArea = 240*PulseSecond;

        /// <summary>
        /// How often characters update.
        /// </summary>
        public const int PulseCharacters = 40*PulseSecond;

        /// <summary>
        /// How long a round of combat updates.
        /// </summary>
        public const int PulseCombat = 4*PulseSecond;

        /// <summary>
        /// How often items update
        /// </summary>
        public const int PulseItems = 40*PulseSecond;

        /// <summary>
        /// For time based actions.
        /// </summary>
        public const int PulseMinute = 60*PulseSecond;

        /// <summary>
        /// How often mobs wander around.
        /// </summary>
        public const int PulseMobWander = 45*PulseSecond;

        /// <summary>
        /// How many cycles per second.
        /// </summary>
        public const int PulseSecond = 1000;

        private static readonly TimerRegistry reg = new TimerRegistry();

        static UpdateManager()
        {
            foreach (Type t in Assembly.GetExecutingAssembly().GetTypes())
            {
                if (!t.IsSubclassOf(typeof (TimerAction)))
                    continue;

                ConstructorInfo ci = t.GetConstructor(Type.EmptyTypes);

                if (ci != null)
                {
                    reg.AddTimerAction((TimerAction) ci.Invoke(Type.EmptyTypes));
                }
            }
        }

        /// <summary>
        /// Updates this instance.
        /// </summary>
        public static void Update()
        {
            reg.Run();
        }
    }
}